<template>
	<view class="app">
		<view class="cell">
			<text class="tit fill">手机号</text>
			<input class="" style="margin: 0 20rpx;flex: 1;" v-model="mobile" type="text" maxlength="16" placeholder="请输入手机号"
				placeholder-style="color:#999;font-size:30rpx" disabled/>
		</view>
		<view class="cell">
			<text class="tit fill">支付密码</text>
			<input class="" style="margin: 0 20rpx;flex: 1;" v-model="paypwd" type="password" maxlength="6" placeholder="请输入新密码"
				placeholder-style="color:#999;font-size:30rpx" />
		</view>
		<view class="cell" style="justify-content: space-between;">
			<view class="" style="display: flex;justify-content: space-between;">
				<text class="tit fill">验证码</text>
				<input style="margin: 0 20rpx;flex: 1;" class="" v-model="code" type="text" maxlength="16"
					placeholder="请输入短信验证码" placeholder-style="color:#999;font-size:30rpx;" />
			</view>
			<view class="">
				<u-code ref="uCode" @change="codeChange" seconds="120" changeText="x秒重新获取"></u-code>
				<u-button type="primary" :text="tips" style="width: 100%;" @tap="getCode"></u-button>
			</view>
		</view>
		<button class="add-btn" style="background: #5990FF;color: #FFFFFF;margin:50px 15px"
			@click="confirm('add')">提交</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				code: '',
				paypwd: '',
				tips: '获取验证码',
				mobile:'',
			}
		},
		onShow() {
			this.getUser()
		},
		methods: {
			async getUser(){
				const res = await this.$fun.ajax.post('user/index', {}) 
				this.mobile = res.data.mobile
			},
			codeChange(text) {
				this.tips = text;
			},
			async getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					const res = await this.$fun.ajax.post('sms/send', {mobile:this.mobile,event:'resetpwd'})
					uni.hideLoading();
					if(res.code==1){
						// 这里此提示会被this.start()方法中的提示覆盖
						uni.$u.toast(res.msg);
						// 通知验证码组件内部开始倒计时
						this.$refs.uCode.start();
					}
					
				} else {
					uni.$u.toast('倒计时结束后再发送');
				}
			},
			async confirm() {
				let {
					mobile,
					code,
					paypwd
				} = this;
				let parms = {
					mobile,
					code,
					paypwd
				}
				if (paypwd == '') {
					this.$fun.msg('请输入支付密码')
					return
				}
				if (code == '') {
					this.$fun.msg('请输入验证码')
					return
				}// user/editPay
				await this.$fun.ajax.post('user/editPay', {
					...parms
				}).then(res => {
					if (res.code == 1) {
						this.$fun.msg(res.msg)
						setTimeout(() => {
							uni.navigateBack()
						}, 800)
					}
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.app {
		padding-top: 30rpx;
	}

	.cell {
		display: flex;
		align-items: center;
		min-height: 110rpx;
		margin: 0 40rpx;
		border-bottom: 1px solid rgb(236, 236, 236);

		&:first-child {
			margin-bottom: 10rpx;
		}

		&:after {
			left: 40rpx;
			right: 40rpx;
			border-color: #d8d8d8;
		}

		.tit {
			width: 120rpx;
			font-size: 30rpx;
			color: #333;
		}

		.avatar-wrap {
			width: 120rpx;
			height: 120rpx;
			position: relative;
			border-radius: 100rpx;
			overflow: hidden;

			.avatar {
				width: 100%;
				height: 100%;
				border-radius: 100rpx;
			}

			.progress {
				position: absolute;
				left: 50%;
				top: 50%;
				transform: translate(-50%, -50%);
				width: 100rpx;
				height: 100rpx;
				box-shadow: rgba(0, 0, 0, .6) 0px 0px 0px 2005px;
				border-radius: 100rpx;
				transition: .5s;
				opacity: 0;

				&.no-transtion {
					transition: 0s;
				}

				&.show {
					opacity: 1;
				}
			}
		}

		.input {
			flex: 1;
			text-align: right;
			font-size: 28rpx;
			color: #333;
		}

		switch {
			margin: 0;
			transform: scale(0.8) translateX(10rpx);
			transform-origin: center right;
		}

		.tip {
			margin-left: 20rpx;
			font-size: 28rpx;
			color: #999;
		}

		.checkbox {
			padding: 12rpx 0 12rpx 40rpx;
			font-size: 28rpx;
			color: #333;

			.mix-icon {
				margin-right: 12rpx;
				font-size: 36rpx;
				color: #ccc;
			}

			.icon-xuanzhong {
				color: $base-color;
			}
		}
	}
</style>